<template>
  <div class="app-container">
    <!--查询表单-->
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item>
        <el-input v-model="conditionQuery.name" placeholder="公司名称"/>
      </el-form-item>

      <el-form-item>
        <el-select v-model="conditionQuery.area" clearable placeholder="所属地区">
          <el-option :value="1" label="昆明"/>
          <el-option :value="2" label="宣威"/>
        </el-select>
      </el-form-item>

      <el-form-item label="时间段">
        <el-date-picker
          v-model="conditionQuery.begin"
          type="datetime"
          placeholder="选择开始时间"
          value-format="yyyy-MM-dd HH:mm:ss"
          default-time="00:00:00"
        />
      </el-form-item>
      <el-form-item>
        <el-date-picker
          v-model="conditionQuery.end"
          type="datetime"
          placeholder="选择截止时间"
          value-format="yyyy-MM-dd HH:mm:ss"
          default-time="00:00:00"
        />
      </el-form-item>

      <el-button type="primary" icon="el-icon-search" @click="getList()">查询</el-button>
      <el-button type="default" @click="resetData()">清空</el-button>
    </el-form>


    <!-- 表格 -->
    <el-table
      :data="list"
      row-key="id"
      @row-click="showDeviceDetail"
      :cell-style="cellStyle"
      :row-class-name="tableRowClassName">

      <el-table-column
        label="序号"
        width="70"
        align="center">
        <!-- 这里的scope可以代表整个表格 -->
        <template slot-scope="scope">
          {{ (page - 1) * size + scope.$index + 1 }}
        </template>
      </el-table-column>

      <el-table-column prop="name" label="公司名称" />

      <el-table-column label="所属地区" width="80">
        <template slot-scope="scope">
          {{ scope.row.area===1?'昆明':'宣威' }}
        </template>
      </el-table-column>

      <el-table-column prop="principal" label="负责人" />

      <el-table-column prop="num" label="购买数" />

      <el-table-column prop="status" label="设备状态"/>

      <el-table-column prop="phone" label="联系方式" width="150"/>

      <el-table-column prop="gmtCreate" label="投放时间" width="160"/>

      <el-table-column prop="remoteControl" label="远程操作" width="80"/>

      <el-table-column label="操作" align="center" width="250">
        <template slot-scope="scope">
          <el-button type="success" size="mini" @click.stop="changeStatus()" round>启用</el-button>
          <router-link :to="'/teacher/edit/'+scope.row.id">
            <el-button type="primary" size="mini" icon="el-icon-edit" round>修改</el-button>
          </router-link>
          <!-- scope.row可以获取表格中每一行的数据-->
          <el-button type="danger" size="mini" icon="el-icon-delete" @click.stop="deleteDeviceInfo()" round>删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--进一步展示设备信息-->
    <el-dialog title="设备详情" :visible.sync="dialogTableVisible">
      <el-table :data="devicesDetail"
                :cell-style="detailCellStyle">
        <el-table-column property="id" label="设备编号" width="150"></el-table-column>
        <el-table-column property="status" label="设备状态" width="100"></el-table-column>
        <el-table-column property="averageSpeed" label="破碎平均转速" width="120"></el-table-column>
        <el-table-column property="meanAirPressure" label="平均气压" width="100"></el-table-column>
        <el-table-column property="averageTorque" label="破碎阀平均扭矩" width="150"></el-table-column>
        <el-table-column property="address" label="设备所在地址" width="400"></el-table-column>
      </el-table>
    </el-dialog>

    <!-- 分页条 -->
    <el-pagination
      :current-page="page"
      :page-size="size"
      :total="total"
      style="padding: 30px 0; text-align: center;"
      layout="total, prev, pager, next, jumper"
      @current-change="getList"
    />
  </div>
</template>

<style>
  .el-table .warning-row {
    background: oldlace;
  }

  .el-table .success-row {
    background: #f0f9eb;
  }
</style>
<script>
  export default {
    //写核心代码的位置
    data() { //定义变量和初始值
      return {
        list: [
          {
            id: 1,
            name: 'aaa有限公司',
            area: 1,
            principal: '张三',
            num: 3,
            status: '正常',
            phone: '13111111111',
            remoteControl: '否',
            gmtCreate: '2021-5-25'
          },
          {
            id: 1,
            name: 'aaa有限公司',
            area: 1,
            principal: '张三',
            num: 3,
            status: '正常',
            phone: '13111111111',
            remoteControl: '否',
            gmtCreate: '2021-5-25'
          },
          {
            id: 1,
            name: 'aaa有限公司',
            area: 1,
            principal: '张三',
            num: 3,
            status: '正常',
            phone: '13111111111',
            remoteControl: '否',
            gmtCreate: '2021-5-25'
          },{
            id: 1,
            name: 'aaa有限公司',
            area: 1,
            principal: '张三',
            num: 3,
            status: '正常',
            phone: '13111111111',
            remoteControl: '否',
            gmtCreate: '2021-5-25'
          },
          {
            id: 1,
            name: 'aaa有限公司',
            area: 1,
            principal: '张三',
            num: 3,
            status: '正常',
            phone: '13111111111',
            remoteControl: '否',
            gmtCreate: '2021-5-25'
          },
          {
            id: 1,
            name: 'aaa有限公司',
            area: 1,
            principal: '张三',
            num: 3,
            status: '正常',
            phone: '13111111111',
            remoteControl: '否',
            gmtCreate: '2021-5-25'
          },
          {
            id: 1,
            name: 'aaa有限公司',
            area: 1,
            principal: '张三',
            num: 3,
            status: '正常',
            phone: '13111111111',
            remoteControl: '否',
            gmtCreate: '2021-5-25'
          },
          {
            id: 1,
            name: 'aaa有限公司',
            area: 1,
            principal: '张三',
            num: 3,
            status: '正常',
            phone: '13111111111',
            remoteControl: '否',
            gmtCreate: '2021-5-25'
          },
          {
            id: 1,
            name: 'aaa有限公司',
            area: 1,
            principal: '张三',
            num: 3,
            status: '正常',
            phone: '13111111111',
            remoteControl: '否',
            gmtCreate: '2021-5-25'
          },
          {
            id: 1,
            name: 'aaa有限公司',
            area: 1,
            principal: '张三',
            num: 3,
            status: '正常',
            phone: '13111111111',
            remoteControl: '否',
            gmtCreate: '2021-5-25'
          }
        ], //查询之后接口返回的集合
        page: 1,//当前页
        size: 10,//每页显示的记录数
        total: 12345,//总记录数
        conditionQuery: {},//条件查询对象
        devicesDetail: [
          {
            id: '791313123123131',
            status: '正常',
            averageSpeed: '132r/min',
            meanAirPressure: '0.8MPa',
            averageTorque: '1375n.m',
            address: '曲靖市宣威市环城东路（华苑小区东南侧约150m）'
          },
          {
            id: '791313123123131',
            status: '异常',
            averageSpeed: '132r/min',
            meanAirPressure: '0.8MPa',
            averageTorque: '1375n.m',
            address: '曲靖市宣威市环城东路（华苑小区东南侧约150m）'
          },
          {
            id: '791313123123131',
            status: '停机',
            averageSpeed: '132r/min',
            meanAirPressure: '0.8MPa',
            averageTorque: '1375n.m',
            address: '曲靖市宣威市环城东路（华苑小区东南侧约150m）'
          }
        ],
        dialogTableVisible: false
      }
    },
    created() {
    },
    methods: {
      //启用设备远程操作
      changeStatus() {
        this.$confirm('确认给该设备开启远程操作权限？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$message({
            type: 'success',
            message: '已启用远程操作!'
          });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '取消操作'
          });
        });
      },
      //根据设备id删除设备信息
      deleteDeviceInfo() {
        this.$confirm('此操作将永久该设备信息, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },
      showDeviceDetail(row) {
        console.log(row);
        this.dialogTableVisible = true
        //根据id查询详细信息
      },
      detailRowClassName({row, rowIndex}) {

      },
      tableRowClassName({row, rowIndex}) {
        if (rowIndex === 1 || rowIndex === 5 || rowIndex === 9) {
          return 'warning-row';
        } else if (rowIndex === 3 || rowIndex === 7) {
          return 'success-row';
        }
        return '';
      },
      cellStyle({ row, column, rowIndex, columnIndex }) {
        // 状态列字体颜色
        if (row.status === '正常' && columnIndex === 5) {
          return 'color: #0CB618'
        } else if (row.status === '异常' && columnIndex === 5) {
          return 'color: #EA1B29'
        } else {
          return 'color: #1a1a1b'
        }
      },
      detailCellStyle({ row, column, rowIndex, columnIndex }) {
        // 状态列字体颜色
        if (row.status === '正常' && columnIndex === 1) {
          return 'color: #0CB618'
        } else if (row.status === '异常' && columnIndex === 1) {
          return 'color: #EA1B29'
        } else {
          return 'color: gray'
        }
      }
    },

  }
</script>

